<?php

class Admin_BoletimdesempenhoController extends App_Controller_Action
{

    private $_table     = 'BOLETIM_DESEMPENHO';
    private $_pk        = 'BTD_ID';
    private $_order_col = 'BTD_ID';
    private $_order     = 'DESC';
    private $_sta_col   = 'BTD_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'BTD_FILE'; 
    public  $dir = 'public/upload//';
    public  $db = '';
    
    public function init()
    {
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Admin_Form_Boletimdesempenho();        
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
        $where = ' a.ALU_ID > 0';
        
        $filtro_nm = $this->_getParam('filtro_nm', '');
        
        if($filtro_nm != '') $where .= ' and a.ALU_NOM like "%' . trim($filtro_nm) .'%"';
        
        $this->view->filtro_nm = $filtro_nm;
        
        //$query = 'SELECT SQL_CACHE * FROM '.$this->_table.' '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        //$result = $this->db->fetchAll($query);
        
        $query = ' SELECT SQL_CACHE distinct(a.ALU_ID), a.ALU_NOM, a.ALU_STA, m.CUR_ID, '
                .' m.MAT_STA, a.ALU_STA, m.MAT_ID, m.MAT_DATHOR'
                .' FROM ALUNO a '
                .' INNER JOIN MATRICULA m ON m.ALU_ID = a.ALU_ID AND MAT_STA = 1 '
                .' INNER JOIN CURSO ON CURSO.CUR_ID = m.CUR_ID AND CUR_STA = 1 AND CUR_ANO = "'.date('Y').'"'
                .' WHERE '.$where.' ORDER BY a.ALU_NOM';
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
    }
    
    public function alunoAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
        $alu_id= $this->_getParam('alu_id', 0);
        
        if($alu_id == 0) $this->_helper->redirector('index');   
        
        $where = ' b.ALU_ID = '.$alu_id;
        
        //$query = 'SELECT SQL_CACHE * FROM '.$this->_table.' '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $query = ' SELECT SQL_CACHE b.*, f.FAC_NOM, p.FAP_NOM'
                .' FROM '.$this->_table.' b, FACULDADE f, FACULDADE_PROVA p '
                .' WHERE '.$where
                .' AND b.FAC_ID = f.FAC_ID '
                .' AND b.FAC_ID = p.FAC_ID AND b.FAP_ID = p.FAP_ID '
                .' ORDER BY b.'.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
        
        $where = ' a.ALU_ID = '.$alu_id;
        
        $query = ' SELECT SQL_CACHE distinct(a.ALU_ID), a.ALU_NOM, a.ALU_STA, m.CUR_ID, '
                .' m.MAT_STA, a.ALU_STA, m.MAT_ID, m.MAT_DATHOR'
                .' FROM ALUNO a '
                .' INNER JOIN MATRICULA m ON m.ALU_ID = a.ALU_ID AND MAT_STA = 1 '
                .' INNER JOIN CURSO ON CURSO.CUR_ID = m.CUR_ID AND CUR_STA = 1 AND CUR_ANO = "'.date('Y').'"'
                .' WHERE '.$where.' ORDER BY a.ALU_NOM';
        $result = $this->db->fetchRow($query);
        $this->view->alu = $result;
        
    }
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_boletimdesempenho.js');
       
        Zend_Loader::loadClass('imagem','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
        $alu_id = $this->_getParam('alu_id', 0);
        
        if($alu_id == 0) $this->_helper->redirector('index');
        
        //$this->view->view = 'view';
        
        $this->view->form = $this->form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if($this->form->isValid($formData)) {
                    
                    $id = (int) $this->form->getValue($this->_pk);
                    
                    $data = $this->form->getValues();
                    
                    unset($data['html']);
                    unset($data['submit']);
                    unset($data['d']);
                    unset($data['n']);
                    unset($data['p']);
                    unset($data['a']);
                    
                    $data['ALU_ID'] = $alu_id;
                    
                    //print_r($formData); exit();
                    $this->db->beginTransaction();
                    if($id == 0){
                        $data[$this->_sta_col] = 1;
                        $this->db->insert($this->_table, $data);
                        $id =  $this->db->lastInsertId();   
                    }
                    else {
                        $data[$this->_sta_col] = $this->form->getValue($this->_sta_col,0);
                        $this->db->update($this->_table, $data,$this->_pk.' = ' . $id);
                    }
                    
                    $this->db->delete('BOLETIM_DESEMPENHO_DISC','BTD_ID = ' . $id);
                    if(isset($formData['d']))
                    foreach($formData['d'] as $key => $d){
                        
                        if($d > 0){

                            $data2 = array(
                                'BTD_ID'    => $id,
                                'DCP_ID'    => $d,
                                'BDD_NQU'   => $formData['n'][$key],
                                'BDD_PES'   => $formData['p'][$key],
                                'BDD_ACT'   => $formData['a'][$key],
                            );

                            $this->db->insert('BOLETIM_DESEMPENHO_DISC',$data2);

                        }
                    }
                    
                    $this->db->commit();
                    
                    $urlOptions = array('controller'=>$this->_controller,'action'=>'aluno','alu_id'=>$alu_id);
                    $this->_helper->redirector->gotoRoute($urlOptions);
                    
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                //Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->form->populate($formData);
                $this->db->rollBack();
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $data = $this->db->fetchRow('SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
                $this->form->populate($data);
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $msg = '';
        $id = $this->_getParam('id', 0);
        if((int) $id > 0 ) {   
            $this->db->beginTransaction();
            try {
                
                $ok = (bool) $this->db->delete('BOLETIM_DESEMPENHO_DISC', 'BTD_ID = ' . $id );

                $ok = (bool) $this->db->delete($this->_table,'BTD_ID = ' . $id );
                
                $this->db->commit();
            } catch(Exception $ex) {
                //echo getMessage();
                $ok = FALSE;
                $msg = MSG_DELETE;
                $this->db->rollBack();
            }             
        }       
        echo json_encode(array('ok' => $ok, 'msg'=> $msg));
        //$this->_helper->redirector('index');
    }
    
}